package com.jianggy.stream;

import java.security.cert.TrustAnchor;
import java.util.*;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author jianggy
 * @version 1.0
 * @time 2019/11/19 11:24
 * @see
 **/
public class SecondDemo {
    public static void main(String[] args) {
        // flatMapTest();
        // mapTest();
        // filterTest();
        // limitTest();
        // test();
        // sortTest();
        // foreachTest();
        // findFirstTest();
        // reduceTest();
        matchTest();
    }

    private static void mapTest() {
        List<String> list = Arrays.asList("china", "beijing", "hebei", "shandong");
        List<String> res;
        //first way
        // res = list.stream().map(new Function<String, String>() {
        //     @Override
        //     public String apply(String s) {
        //         return s.toUpperCase();
        //     }
        // }).collect(Collectors.toList());
        //second way
        // res = list.stream().map(s -> s.toUpperCase()).collect(Collectors.toList());
        //third way
        res = list.stream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(res);
    }

    private static void flatMapTest() {
        List<List<String>> list = Arrays.asList(Arrays.asList("1", "2", "3", "4"), Arrays.asList("5", "6", "7", "8"));
        Stream<List<String>> stream = list.stream();
        //first way
        // List<String> res = stream.flatMap(new Function<List<String>, Stream<String>>() {
        //     @Override
        //     public Stream<String> apply(List<String> strings) {
        //         return strings.stream();
        //     }
        // }).collect(Collectors.toList());
        // System.out.println(res);

        //second way

        // List<String> res = stream.flatMap(strings -> strings.stream()).collect(Collectors.toList());
        // System.out.println(res);
        //third way
        List<String> res = stream.flatMap(List::stream).collect(Collectors.toList());
        System.out.println(res);
    }


    private static void filterTest() {
        //odd
        IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        stream.filter(value -> (value % 2) == 1).forEach(value -> System.out.print(value + ","));

        //event
        System.out.println();
        IntStream stream1 = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        stream1.filter(new IntPredicate() {
            @Override
            public boolean test(int value) {
                if (value % 2 == 0) {
                    return true;
                }
                return false;
            }
        }).forEach(new IntConsumer() {
            @Override
            public void accept(int value) {
                System.out.print(value + ",");
            }
        });
    }

    private static void limitTest() {
        List<String> list = Arrays
                .asList("name1", "name2", "name3", "name4", "name5", "name6", "name7", "name8", "name9", "name10");
        List res = list.stream().limit(5).collect(Collectors.toList());
        System.out.println(res);
        res = list.stream().skip(5).collect(Collectors.toList());
        System.out.println(res);
    }

    private static void sortTest() {
        List<String> list = Arrays.asList("1bc", "0cd", "9g2", "2fa", "6bc", "7ta");
        list.stream().sorted((String o1, String o2) -> {

            if (Integer.parseInt(o1.substring(0, 1)) > Integer.parseInt(o2.substring(0, 1)))
                return 1;
            return -1;
        }).forEach(s -> System.out.print(s + ","));

    }

    private static void foreachTest() {
        List<String> list = Arrays.asList("1bc", "0cd", "9g2", "2fa", "6bc", "7ta");
        list.stream().forEach(s -> System.out.print(s + ","));

    }

    private static void peekTest() {
        List<String> list = Arrays.asList("1bc", "0cd", "9g2", "2fa", "6bc", "7ta");
        list.stream().peek(s -> System.out.println(s + ""));
    }

    private static void findFirstTest() {
        List<String> list = Arrays.asList("1bc", "0cd", "9g2", "2fa", "6bc", "7ta");
        Optional<String> optional = list.stream().findFirst();
        System.out.println(optional.get());

        String res = list.stream().findAny().orElse(null);
        System.out.println(res);
    }

    private static void reduceTest() {
        IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7);
        OptionalInt optional = stream.reduce((int left, int right) -> {
            return left + right;
        });
        System.out.println(optional.getAsInt());
    }


    private static void matchTest(){
        IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7);
        boolean res1 = stream.allMatch((int i)->i>5);
        System.out.println(res1);

        stream = IntStream.of(1, 2, 3, 4, 5, 6, 7);
        boolean res2 = stream.anyMatch((int i)->i>5);
        System.out.println(res2);

        stream = IntStream.of(1, 2, 3, 4, 5, 6, 7);
        boolean res3 = stream.noneMatch((int i)->i>5);
        System.out.println(res3);
    }


}
